﻿#include "MyGDIPlus.h"
#include "MyCImg.h"
#include "MyCairo.h"

#include "MyLine.h"
#include "MyRectangle.h"
#include "MyEllipse.h"
#include "MyTriangle.h"
#include "MyEffect.h"

#include "pugixml.hpp"
using namespace pugi;

void main()
{	
	HWND console =  GetConsoleWindow();
	MyGraphic* graphic = new MyGDIPlus(console);
	//MyGraphic* graphic = new MyCImg(800, 600, 1, 255, "CImg Demo");
	//MyGraphic* graphic = new MyCairo(GetDC(GetConsoleWindow()));


	//Đọc tập tin *.svg và chuyển nội dung thành danh sách các con trỏ kiểu MyShape
	vector<MyShape*> shapes = MyShape::Parse("image.svg");
	//Duyệt qua tất cả phần tử, gán loại thư viện muốn dùng và vẽ ra màn hình
	for(unsigned int i = 0; i < shapes.size(); ++i) {
		shapes[i]->SetMyGraphic(graphic);
		shapes[i]->Draw();
	}
	
	vector<MyShape*> newShapes;
	char color[] = { 125, 0, 255 };
	char colorFill[] = { 150, 255, 128};	

	MyShape* line = new MyLine(color, 5.0F, 200, 200, 400, 200);
	line->SetMyGraphic(graphic);
	line->Draw();	
	newShapes.push_back(line);

	MyShape* rect = new MyRectangle(color, colorFill, 2.5F, 30, 75, 200, 100);
	//MyShape* rect = new MyRectangle(color, NULL, 2.5F, 50, 50, 200, 100);
	rect->SetMyGraphic(graphic);
	rect->Draw();	
	newShapes.push_back(rect);


	MyEffect* border = new MyBorder();

	rect->SetEffect(border);


	//MyShape* ellipse = new MyEllipse(color, colorFill, 3.5F, 400, 75, 100, 50);
	////MyShape* ellipse = new MyEllipse(color, NULL, 3.5F, 400, 75, 100, 50);
	//ellipse->SetMyGraphic(graphic);
	//ellipse->Draw();
	//newShapes.push_back(ellipse);

	////Lưu danh sách các hình ra tập tin
	//MyShape::Save(newShapes, "test.svg");

	delete line;
	delete rect;
	//delete ellipse;
	
	delete graphic;
}

vector<MyShape*> MyShape::Parse(string filename) {
	vector<MyShape*> shapes;
	
	pugi::xml_document doc;
	std::ifstream stream(filename);
	pugi::xml_parse_result result = doc.load(stream);
	for (pugi::xml_node shape = doc.child("svg").first_child(); shape; shape = shape.next_sibling())
	{
		string type = shape.name();
		if(type == "line") {
			int x1 = shape.attribute("x1").as_int();
			int y1 = shape.attribute("y1").as_int();
			int x2 = shape.attribute("x2").as_int();
			int y2 = shape.attribute("y2").as_int();
			char* stroke = MyFunction::ParseColor(shape.attribute("stroke").value());
			float strokeWidth = shape.attribute("stroke-width").as_float();
			
			MyShape* shape = new MyLine(stroke, strokeWidth, x1, y1, x2, y2);
			shapes.push_back(shape);
			//cout << x1 << endl << y1 << endl << x2 << endl << y2 << endl << stroke << endl;
		}
		else if(type == "rect") {
			int x = shape.attribute("x").as_int();
			int y = shape.attribute("y").as_int();
			int width = shape.attribute("width").as_int();
			int height = shape.attribute("height").as_int();			
			char* stroke = MyFunction::ParseColor(shape.attribute("stroke").value());
			char* fill = MyFunction::ParseColor(shape.attribute("fill").value());
			float strokeWidth = shape.attribute("stroke-width").as_float();
			
			MyShape* shape = new MyRectangle(stroke, fill, strokeWidth, x, y, width, height);
			shapes.push_back(shape);
			//cout << x << endl << y << endl << width << endl << height << endl << stroke << endl << strokeWidth << endl << fill << endl ;
		}
		else if(type == "ellipse") {
			int cx = shape.attribute("cx").as_int();
			int cy = shape.attribute("cy").as_int();
			int rx = shape.attribute("rx").as_int();
			int ry = shape.attribute("ry").as_int();
			char* stroke = MyFunction::ParseColor(shape.attribute("stroke").value());
			char* fill = MyFunction::ParseColor(shape.attribute("fill").value());
			float strokeWidth = shape.attribute("stroke-width").as_float();
			
			MyShape* shape = new MyEllipse(stroke, fill, strokeWidth, cx, cy, rx, ry);
			shapes.push_back(shape);
			//cout << cx << endl << cy << endl << rx << endl << ry << endl << stroke << endl << strokeWidth << endl << fill << endl ;
		}		
	}
	return shapes;
}